﻿public class Class1
{
	public void WindowContainerBasicStuff()
	{
		IWindsorContainer container = new WindsorContainer(new XmlInterpreter());

		FileReader defaultReader = container.Resolve<FileReader>();
		FileReader file1Reader = container.Resolve<FileReader>("reader.file1");

		container.AddComponent<IEmailSender, MyEmailSender>();

		// or with specific key:
		container.AddComponent<IEmailSender, MyEmailSender>("key");

		Foo[] foos = container.ResolveAll<Foo>();
	}

	public void FluentRegistration1(IWindsorContainer container)
	{
		container.Register(
			Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
			Component.For<IMyService>().ImplementedBy<OtherServiceImpl>()
		);

		container.Register(
			Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHRepository<>))
		);

		container.AddFacility<FactorySupportFacility>()
		   .Register(
			  Component.For<IMyService>().UsingFactoryMethod(
				() => MyLegacyServiceFactory.CreateMyService())
		);

		container.AddFacility<FactorySupportFacility>()
		   .Register(
				Component.For<IMyFactory>().ImplementedBy<MyFactory>(),
				Component.For<IMyService>().UsingFactoryMethod(
					kernel => kernel.Resolve<IMyFactory>().Create())
		);

		container.Register(
		   Component.For<IService>().ImplementedBy<MyService>()
			  .OnCreate((kernel, instance) => instance.Name += "a")
		);
	}

	public void FluentRegistration_StaticDependencies(IWindsorContainer container)
	{
		// Supplying static dependencies:
		DateTime time2001 = new DateTime(2001, 2, 3, 18, 0, 0);

		// These registrations all configure the clock with the same parameter.
		container.Register(

			Component.For<IClock>().ImplementedBy<StaticClock>()
				.Named("clock1")
				.DependsOn(Property.ForKey("time").Eq(time2001)),

			Component.For<IClock>().ImplementedBy<StaticClock>()
				.Named("clock2")
				.DependsOn(new { time = time2001 }),

			Component.For<IClock>().ImplementedBy<StaticClock>()
				.Named("clock3")
				.DependsOn(new Hashtable { { "time", time2001 } })
		);
	}

	public void FluentRegistration_DynamicDependencies(IWindsorContainer container)
	{
		container.Register(
			Component.For<ClassWithArguments>()
				.LifeStyle.Transient
				.DynamicParameters((kernel, paramDictionary) =>
					paramDictionary["createdTimestamp"] = DateTime.Now)
			);
	}

	public void FluentRegistration_RegisteringMultipleTypes(IWindsorContainer container)
	{
		container.Register(
			AllTypes.Of<Controller>()
				.FromAssembly(Assembly.GetExecutingAssembly())
		);

		container.Register(
			AllTypes.FromAssemblyContaining<DeleteCustomerCommand>()
				.BasedOn<IEntity>()
				.BasedOn<IHandler>()
			);
	}

	public void FluentRegistration_ConditionalRegistration(IWindsorContainer container)
	{
		container.Register(
			AllTypes.Of<ICustomer>()
				.FromAssembly(Assembly.GetExecutingAssembly())
				.Unless(t => typeof(SpecificCustomer).IsAssignableFrom(t))
			);

		container.Register(
			AllTypes.Of<CustomerChain1>()
				.Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
					  where type.IsDefined(typeof(SerializableAttribute), true)
					  select type
				)
			);
	}
}
